﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using System;
	using Falcon.Core.Collections;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Extensions;

	public class CommentService : ICommentService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="CommentService"/> class.
		/// </summary>
		/// <param name="commentRepository">The comment repository.</param>
		public CommentService(ICommentRepository commentRepository)
		{
			this.CommentRepository = commentRepository;
		}

		/// <summary>
		/// Gets or sets the comment repository.
		/// </summary>
		/// <value>The comment repository.</value>
		public ICommentRepository CommentRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the <see cref="Comment"/> by the specified specification.
		/// </summary>
		/// <param name="specification">The <see cref="CommentSpecification"/>.</param>
		/// <returns></returns>
		public Comment GetBy(CommentSpecification specification)
		{
			return this.CommentRepository.GetBy(specification);
		}

		/// <summary>
		/// Gets the <see cref="Comment"/> by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public Comment GetById(int id)
		{
			return this.CommentRepository.GetById(id);
		}

		/// <summary>
		/// Gets all the comments by the specified specification.
		/// </summary>
		/// <param name="specification">The <see cref="CommentSpecification"/>.</param>
		/// <returns></returns>
		public IPagedList<Comment, CommentSpecification> GetPaged(CommentSpecification specification)
		{
			return this.CommentRepository.GetPaged(specification);
		}

		/// <summary>
		/// Creates the <see cref="Comment"/>.
		/// </summary>
		/// <returns></returns>
		public Comment Create()
		{
			var comment = new Comment
			{
				Created = DateTime.Now
			};

			comment.Updated = comment.Created;

			return comment;
		}

		/// <summary>
		/// Inserts the specified <see cref="Comment"/>.
		/// </summary>
		/// <param name="comment">The <see cref="Comment"/>.</param>
		public void Insert(Comment comment)
		{
			comment.Validate();

			comment.Slug = comment.Title.Slug();
			comment.BodyParsed = comment.Body.BBCodeToHtml(true);

			this.CommentRepository.Insert(comment);
		}

		/// <summary>
		/// Updates the specified <see cref="Comment"/>.
		/// </summary>
		/// <param name="comment">The <see cref="Comment"/>.</param>
		public void Update(Comment comment)
		{
			comment.Validate();

			comment.Slug = comment.Title.Slug();
			comment.BodyParsed = comment.Body.BBCodeToHtml(true);

			this.CommentRepository.Update(comment);
		}

		/// <summary>
		/// Deletes the specified <see cref="Comment"/>.
		/// </summary>
		/// <param name="comment">The <see cref="Comment"/>.</param>
		public void Delete(Comment comment)
		{
			this.CommentRepository.Delete(comment);
		}
	}
}